<?php
class Curl extends CI_Controller {


    public function curl_https($url, $cookie_file = '') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_URL, $url);
        // 设定curl不返回头信息
        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 携带cookie访问指定页面
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

        $response = curl_exec($ch);

        if ($error = curl_error($ch)) {
            die($error);
        }
        curl_close($ch);
        return $response;
    }


    public function getCookieByHttps($url = '') {
        // 指定cookie文件存放地址
        // var_dump(sys_get_temp_dir());
        $cookie_file = tempnam(sys_get_temp_dir(), "cookies");
        $ch = curl_init($url); //初始化
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串，而非直接输出
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); //存储cookies到指定的临时文件中
        curl_exec($ch);
        curl_close($ch);
        return $cookie_file;
    }

    public function test()
    {
        // 取消页面执行时间的限制
        set_time_limit(0);
       $cookie_file = $this->getCookieByHttps('https://xueqiu.com/hq#exchange=CN&firstName=1&secondName=1_0');
        $string = $this->curl_https('https://xueqiu.com/stock/cata/stocklist.json?page=1&size=30&order=desc&orderby=percent&type=11%2C12&_=1496819576229',$cookie_file);
        $data = json_decode($string);
        var_dump($data);
    }

    //将查询结果存储到数据库
    public function saveXueqiuData()
    {
        // 创建数据库
        $this->load->model('Stocks');
        $this->Stocks->resetStocksTable();
    }

    public function GetTotalPages() {
        // 取得cookie文件
        $cookie_file = $this->getCookieByHttps('https://xueqiu.com/hq#exchange=CN&firstName=1&secondName=1_0');
        // var_dump($cookie_file);
        // 抓取数据
        $string = $this->curl_https('https://xueqiu.com/stock/cata/stocklist.json?page=1&size=30&order=desc&orderby=percent&type=11%2C12&_=1475149781606', $cookie_file);
        // var_dump($string);
        $array = json_decode($string, true);
        return $array['count']['count'];
    }

    public function getAllStockName() {
        // 取消页面执行时间的限制
        set_time_limit(0);
        // 获取指定页面存储在本地的cookie信息
        $cookie_file = $this->Curlmodel - getCookieByHttps('https://xueqiu.com/hq#exchange=CN&firstName=1&secondName=1_0');
        // 获取全部页面的数量
        $totalPages = ceil($this->Curlmodel->GetTotalPages() / 90);
        // 获取行情中心的股票信息
        for ($i = 1; $i <= $totalPages; $i++) {
            $url = 'http://xueqiu.com/stock/cata/stocklist.json?page=' . $i . '&size=90&order=desc&orderby=percent&type=11%2C12&_=1447474625890';
            // 后面类似时间戳的字符串需要更新
            // http://xueqiu.com/stock/cata/stocklist.json?page=1&size=30&order=desc&orderby=percent&type=11%2C12&_=1447474625890
            $this->getFromXueqiu($url, $cookie_file);
        }
        // 调用自定义函数显示信息，并跳转
        message('Curl/index', '抓取成功，接下来应该清理数据了。');

    }
    /**
     * [getFromXueqiu 从雪球抓取指定指定页面中的代码信息]
     * @param  string $url [description]
     * @param  string $cookie_file [description]
     * @return [type]              [description]
     */
    public function getFromXueqiu($url = '', $cookie_file = '') {
        $string = $this->Curlmodel->curl_https($url, $cookie_file);
        $array = json_decode($string, true);
        foreach ($array['stocks'] as $value) {
            $data = array(
                'symbol' => $value['symbol'],
                'scode' => $value['code'],
                'sname' => $value['name'],
            );
            $this->Stocks->replaceStockName($data);
            // 使用replace一个语句可以完成上面两个语句的功能,其语法与insert差不多.
            // 如上面的操作可以写为replace into test set id=1,value='a',icount=0;
            // 则表中有id为1时,先删除旧数据.然后插入新数据.否则直接插入数据.
        }
    }





}